import requests
from requests.packages import urllib3
import json
from orm.TKzzFx import TKzzFx


def refresh_kzz():
    urllib3.disable_warnings()
    data = {'rp': 50, 'listed': 'Y'}
    req = requests.post('https://www.jisilu.cn/data/cbnew/cb_list/', data=data, verify=False)
    print(req)
    js = json.loads(req.text)
    rows = js['rows']
    print(js['total'])
    # for row in rows[:10]:
    for row in rows:
        cell = row['cell']
        zzdm = cell['bond_id']
        print('%s' % zzdm)
        # cursor.execute("SELECT * from t_kzz_fx t where t.`转债代码`= %s " % (zzdm))
        # 回售日期
        next_put_dt = cell['next_put_dt']
        # 正股价格
        sprice = float(cell['sprice'])
        # 触发回售价格
        # cf_sprice = cell['put_convert_price'] if cell['put_convert_price']=='-' else float(cell['put_convert_price'])
        # 回售价格
        print('%s' % cell['put_price'] is None)
        hs_sprice = cell['put_price'] if cell['put_price'] is None else float(cell['put_price'])
        # 现价
        price = float(cell['price'])
        # 回售久期
        hs_jq = float(cell['year_left'])
        if (hs_jq > 2):
            hs_jq = hs_jq - 2
        elif (hs_jq > 1):
            hs_jq = hs_jq - 1
        # 回售利息
        put_lx = 1.2
        if(hs_sprice is not None):
            ytm_hs = (hs_sprice - price + put_lx) / price / hs_jq
            print('%s' % ytm_hs)
        kzz = TKzzFx.select().where(TKzzFx.zzdm == zzdm)
        # print(kzz)
        if kzz:

            TKzzFx.update({
                TKzzFx.xj: cell['price'], TKzzFx.zdf: cell['increase_rt'], TKzzFx.zgj: cell['sprice'],
                TKzzFx.zgzd: cell['sincrease_rt'], TKzzFx.pb: cell['pb'],
                TKzzFx.zgjg: cell['convert_price'], TKzzFx.zgjz: cell['convert_value'], TKzzFx.yjl: cell['premium_rt'],
                TKzzFx.czjz: cell['bond_value'], TKzzFx.pj: cell['rating_cd'],
                TKzzFx.qqjz: 'buy', TKzzFx.hscfj: cell['put_convert_price'], TKzzFx.qscfj: cell['force_redeem_price'],
                TKzzFx.zzzb: cell['convert_amt_ratio'], TKzzFx.jgcc: cell['fund_rt'],
                TKzzFx.dqsj: cell['short_maturity_dt'], TKzzFx.synx: cell['year_left'],
                TKzzFx.sygm: cell['curr_iss_amt'], TKzzFx.cje: cell['volume'], TKzzFx.hsl: cell['turnover_rt'],
                TKzzFx.dqsqsy: cell['ytm_rt'], TKzzFx.hssy: '**', TKzzFx.sd: cell['dblow'],
                TKzzFx.convert_cd_tip: cell['convert_cd_tip'], TKzzFx.force_redeem: cell['force_redeem'],
                TKzzFx.dqshsy: cell['ytm_rt_tax'],TKzzFx.hs_sy: ytm_hs
            }).where(TKzzFx.zzdm == zzdm).execute()

        else:

            new_kzz = TKzzFx(zzdm=zzdm, zzmc=cell['bond_nm'], xj=cell['price'],
                             zdf=cell['increase_rt'], zgmc=cell['stock_nm'],
                             zgj=cell['sprice'], zgzd=cell['sincrease_rt'], pb=cell['pb'], zgjg=cell['convert_price'],
                             zgjz=cell['convert_value'],
                             yjl=cell['premium_rt'], czjz=cell['bond_value'], pj=cell['rating_cd'], qqjz='',
                             hscfj=cell['put_convert_price'],
                             qscfj=cell['force_redeem_price'], zzzb=cell['convert_amt_ratio'], jgcc=cell['fund_rt'],
                             dqsj=cell['short_maturity_dt'], synx=cell['year_left'],
                             sygm=cell['curr_iss_amt'], cje=cell['volume'], hsl=cell['turnover_rt'],hs_sy=ytm_hs,
                             dqsqsy=cell['ytm_rt'], dqshsy=cell['ytm_rt_tax'], hssy='**',
                             sd=cell['dblow'], convert_cd_tip=cell['convert_cd_tip'], force_redeem=cell['force_redeem'])
            new_kzz.save()


if __name__ == '__main__':
    refresh_kzz();
